﻿/*
 * 2023/4/20
 * Dapper帮助类
 * 需要安装包：
 * 1、Dapper
 * 2、SqlClient/MysqlClient
 * 
 * 
 * 
 * **/

using Dapper;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH6_2009A.LowCode.DataAccess
{
    public class DapperFactory
    {
        IConfiguration _config;
        string _connectionString;
        string _databaseType;
        public DapperFactory(IConfiguration config)
        {
            _config = config;
            _databaseType = _config.GetSection("Database:DatabaseType").Value;

        }

        public  IDbConnection GetIDbConnection()
        {
            IDbConnection connection = null;
            switch(_databaseType.ToLower())
            {
                case "mysql":
                    _connectionString = _config.GetSection("Database:MySql:ConnectionString").Value;
                    connection = new MySqlConnection(_connectionString);
                    break;
                case "sqlserver":
                    _connectionString = _config.GetSection("Database:SqlServer:ConnectionString").Value;
                    connection = new SqlConnection(_connectionString);
                    break;
            }

            return connection;
        }
    }
}
